package GUI;

import game.Tile;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import util.INIFile;
import util.INIGroup;

public class Backgrounds {
	private static Backgrounds instance = null;
	private HashMap<String, BufferedImage> backs = new HashMap<String, BufferedImage>();
	private BufferedImage def = null;
	
	@SuppressWarnings({ "rawtypes" })
	private Backgrounds()
	{
		try {
			INIFile ini = new INIFile("Data" + File.separator + "backgrounds.txt");
			INIGroup g = null;
			
			while((g = ini.getNext()) != null)
			{
				if("BattleBackgrounds".equals(g.title))
				{
				    Set set = g.vals.entrySet();
				    Iterator itr = set.iterator();
				    
				    while(itr.hasNext())
				    {
				    	Map.Entry ent = (Map.Entry)itr.next();
				    	if("default".equals(ent.getKey().toString()))
				    	{
				    		def = ImageStorage.getImage("GameGFX/Backgrounds/" + ent.getValue().toString());
				    	}
				    	else
				    	{
				    		BufferedImage img = ImageStorage.getImage("GameGFX/Backgrounds/" + ent.getValue().toString());
				    		if(img != null) backs.put(ent.getKey().toString(), img);
				    	}
				    }
				}
			}
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	private static Backgrounds get()
	{
		if(instance == null) instance = new Backgrounds();
		return instance;
	}
	
	public static BufferedImage getForTile(Tile t)
	{
		if(t == null) return get().def;
		return getByName(t.getBackName());
	}
	
	public static BufferedImage getByName(String name)
	{
		BufferedImage img = get().backs.get(name);
		if(img != null) return img;
		return get().def;
	}
}
